/**
 * Created by John on 2016/7/11.
 */
var mongo = new Mongo('localhost');
var wordsDB = mongo.getDB('BeginningMongoDB');
var wordsColl = wordsDB.getCollection('word_stats');

var cursor = wordsColl.group({
    key: {first: 1, last: 1},
    coud: {first: 'a', last: {$in: ['a', 'e', 'i', 'o', 'u']}},
    initial: {'count': 0},
    reduce: function (obj, prev) {
        prev.count++;
    }
});
print('"A" words grouped by first and last letter that end with a vowel:');
cursor.forEach(function (item) {
    print(JSON.stringify(item));
});

cursor = wordsColl.group({
    key: {first: 1},
    coud: {size: {$gt: 13}},
    initial: {'count': 0, 'totalVowels': 0},
    reduce: function (obj, prev) {
        prev.count++;
        prev.totalVowels += obj.stats.vowels;
    }
});
print('Words larger than 13 character grouped by first letter:');
cursor.forEach(function (item) {
    print(JSON.stringify(item));
});

cursor = wordsColl.group({
    key: {first: 1},
    coud: {},
    initial: {'count': 0, 'vowels': 0, 'consonants': 0},
    reduce: function (obj, prev) {
        prev.count++;
        prev.vowels += obj.stats.vowels;
        prev.consonants += obj.stats.consonants;
    },
    finalize: function (obj) {
        obj.total = obj.vowels + obj.consonants;
    }
});
print('Words grouped by first letter with totals');
cursor.forEach(function (item) {
    print(JSON.stringify(item));
});